@echo off

rem ---------------
rem Local variables
rem ---------------

rem @set D_VER=D12
@set D_VER=%1

@set DCC=%2
@set BPL=%3
@set DCP=%4

@set inst_log=%5

@if not defined DCC goto ParamsError
@if not defined BPL goto ParamsError
@if not defined DCP goto ParamsError

@if not exist %BPL% mkdir %BPL%
@if not exist %DCP% mkdir %DCP%

@if exist %BPL%\QBuilder??_%D_VER%.bpl del %BPL%\QBuilder??_%D_VER%.bpl /Q
@if exist %DCP%\QBuilder??_%D_VER%.dcp del %DCP%\QBuilder??_%D_VER%.dcp /Q

rem ------------------
rem Deleting old files
rem ------------------

@if exist error.log del error.log

@if exist *.~?? del *.~?? > nul
@if exist *.dcu del *.dcu > nul

rem --------------------------
rem Compiling run-time package
rem --------------------------

rem @if exist QBuilderRT_%D_VER%.dpk %DCC% QBuilderRT_%D_VER%.dpk -B -LN%DCP% >> error.log
@%DCC% QBuilderRT_%D_VER%.dpk -B >> error.log
@if errorlevel=1 goto RTCommonError
@echo QBuilderRT_%D_VER%.dpk was compiled successfully! >>error.log

rem -----------------------------
rem Compiling design-time package
rem -----------------------------

rem @if exist QBuilderDT_%D_VER%.dpk %DCC% QBuilderDT_%D_VER%.dpk -B -LN%DCP% -U%DCP%>> error.log
@%DCC% QBuilderDT_%D_VER%.dpk -B >> error.log
@if errorlevel=1 goto DTCommonError
@echo QBuilderDT_%D_VER%.dpk was compiled successfully! >>error.log

rem --------------------
rem Copying bpl files
rem --------------------

@copy *%D_VER%.bpl %BPL% /Y > nul

rem -----------------------
rem Deleting needless files
rem -----------------------

@if exist *.bpl del *.bpl > nul
@if exist *.dcp del *.dcp > nul
@if exist *.dcu del *.dcu > nul

goto End

:RTCommonError
@echo QBuilderRT_%D_VER%.dpk was compiled with errors! >>error.log
@echo QBuilderRT_%D_VER%.dpk >>%inst_log%
rem @"error.log"
rem @pause
@exit

:DTCommonError
@echo QBuilderDT_%D_VER%.dpk was compiled with errors! >>error.log
@echo QBuilderDT_%D_VER%.dpk >>%inst_log%
rem @"error.log"
rem @pause
@exit

:ParamsError
@echo Empty params!
rem @pause
@exit

:End
